﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CarbonExchange.AmeeQuery;
using System.Xml.Linq;

namespace CarbonExchange.Bll
{
    public class AmeeProfileFactory : AmeeFactoryBase<AmeeProfile>
    {
        public AmeeProfileFactory() : base() { }
        public AmeeProfileFactory(IAmeeCommand command) : base(command) { }

        public AmeeProfile GetById(string id)
        {
            var tmp = Command.SendXmlGet(base.BuildAmeeProfilePath(id, "/Metadata"));
            var root = tmp.Root.Elements("ProfileCategoryResource").FirstOrDefault();
            if (root != null)
            {
                var metaItem = root.Element("Children").Element("ProfileItems").Elements("ProfileItem").FirstOrDefault();
                var tmpNoOfPeople = metaItem.Element("peopleInHousehold").Value;
                var tmpStartDate = metaItem.Element("validFrom").Value;
                return new AmeeProfile()
                {
                    Id = root.Element("Profile").Attribute("uid").Value,
                    StartDate = base.ConvertToAmeeDate(tmpStartDate),
                    PeopleInHoushold = string.IsNullOrEmpty(tmpNoOfPeople) ? 0 : int.Parse(tmpNoOfPeople),
                    Country = string.IsNullOrEmpty(metaItem.Element("country").Value) ? null : metaItem.Element("country").Value,
                    State = string.IsNullOrEmpty(metaItem.Element("state").Value) ? null : metaItem.Element("state").Value,
                    MetaDataId = metaItem.Attribute("uid").Value
                };
            }
            else
            {
                return null;
            }
        }

        public void Save(AmeeProfile item)
        {
            Dictionary<string, string> data = new Dictionary<string, string>();
            data.Add("validFrom", ConvertAmeeDate(item.StartDate));
            data.Add("peopleInHousehold", item.PeopleInHoushold.ToString());
            data.Add("country", item.Country);
            data.Add("state", item.State);
            XDocument updated = null; 
            if (string.IsNullOrEmpty(item.Id))
            {
                var newId = Command.CreateProfile("testing");
                item.Id = newId.Root.Elements().First().Element("Profile").Attribute("uid").Value;

                updated = Command.SendXmlInsert(base.BuildAmeeProfilePath(item.Id, "/metadata"), "86D02FBD95AE", data);

                item.MetaDataId = updated.Root.Elements().First().Element("ProfileItem").Attribute("uid").Value;
            }
            else
            {
                updated = Command.SendXmlUpdate(base.BuildAmeeProfilePath(item.Id, "/metadata", item.MetaDataId), data);
            }
            item.StartDate = GetProfileItemValidFrom(updated);
            item.Country = GetProfileItemValue(updated, "country");
            item.PeopleInHoushold = int.Parse(GetProfileItemValue(updated, "peopleInHousehold"));
        }

        protected DateTime GetProfileItemValidFrom(XDocument updated)
        {
            var tmp = updated.Root.Elements().First().Element("ProfileItem").Element("ValidFrom").Value;
            
            return ConvertToAmeeDate(tmp);
        }

        protected string GetProfileItemValue(XDocument updated, string name)
        {
            var tmp = updated.Root.Elements().First().Element("ProfileItem").Element("ItemValues").Elements("ItemValue").Where(x => x.Element("Path").Value == name).FirstOrDefault();
            if (tmp != null)
            {
                return tmp.Element("Value").Value;
            }
            else
            {
                return null;
            }
        }

        private string ConvertAmeeDate(DateTime dateTime)
        {
            return dateTime.ToString("yyyyMMdd");
        }


        public void Delete(AmeeProfile tmp)
        {
            if (!string.IsNullOrEmpty(tmp.Id))
            {
                if (!string.IsNullOrEmpty(tmp.MetaDataId))
                {
                    Command.SendXmlDelete(BuildAmeeProfilePath(tmp.Id, "/metadata", tmp.MetaDataId));
                }
                Command.SendXmlDelete(BuildAmeeProfilePath(tmp.Id, null));
            }
        }
    }


}
